package org.one.afternoon.mapper;

import org.apache.ibatis.annotations.*;
import org.one.afternoon.domain.User;
import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select * from user where id=#{id}")
    User findUserById(Integer id);


    @Select({"<script>",
            "select * from user where 1=1",
            "<when test='item.name!=null and item.name!=\"\"'>and name=#{item.name}</when>",
            "<when test='item.sex!=null and item.sex!=\"\"'>and sex=#{item.sex}</when>",
            "<when test='item.age!=null and item.age!=\"\"'>and age=#{item.age}</when>",
            "<when test='item.role!=null and item.role!=\"\"'>and role=#{item.role}</when> </script>"})
    List<User> findUser(@Param("item") User user);


    @Select("select * from user where username=#{username}")
    User findUserByUsername(String username);

    @Insert("insert into user(name,age,sex,role,create_at) values (#{name},#{age},#{sex},#{role},#{create_at})")
    void addUser(User user);

    @Insert("insert into user (username,password,role,create_at) values (#{username},#{password},#{role},#{create_at})")
    void addAdminUser(User user);

    @Update("update user set name=#{item.name},sex=#{item.sex},age=#{item.age},role=#{item.role} where id=#{item.id}")
    void updateUser(@Param("item") User user);


    @Delete("delete from user where id=#{id}")
    void deleteUserById(Integer id);

    @Select("select * from user")
    List<User> findAll();

    @Select("select * from user where username=#{username} and password=#{password}")
    User login(User user);

    //查找登陆人数
    @Select("select COUNT(*) from user where online = 1")
    int findOnline();

    //修改登录态
    @Update("update user set online=#{status} where name=#{name}")
    void updateOnline(int status,String name);

    //查找用户总数
    @Select("select COUNT(*) from user")
    int findTotal();

    //查找今日新增
    @Select("select COUNT(*) from user where DAY(create_at)=#{time}")
    int findTdy(int time);

    //查找今日修改
    @Select("select COUNT(*) from user where DAY(update_at)=#{time}")
    int findNew(int time);
}
